package com.gnerv.sylvanas.business.manage.controller;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.business.manage.logic.AccountLogicService;
import com.gnerv.sylvanas.business.manage.model.dto.AccountCreateDTO;
import com.gnerv.sylvanas.business.manage.model.dto.AccountDTO;
import com.gnerv.sylvanas.business.manage.model.dto.AccountDetailsDTO;
import com.gnerv.sylvanas.business.manage.model.dto.AccountRoleDTO;
import com.gnerv.sylvanas.business.manage.model.dto.AccountUpdateDTO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping(value = "/api/manage/account")
public class AccountController {

    private final AccountLogicService accountLogicService;

    @PostMapping(value = "")
    public RestResult<Void> create(@RequestBody AccountCreateDTO accountCreateDTO) {
        return accountLogicService.create(accountCreateDTO);
    }

    @DeleteMapping(value = "")
    public RestResult<Void> deleteByUkId(@RequestParam(value = "ukId") String ukId) {
        return accountLogicService.deleteByUkId(ukId);
    }

    @PutMapping(value = "")
    public RestResult<Void> updateByUkId(@RequestBody AccountUpdateDTO accountUpdateDTO) {
        return accountLogicService.updateByUkId(accountUpdateDTO);
    }

    @PutMapping(value = "/bind/role")
    public RestResult<Void> bindRole(@RequestBody AccountRoleDTO accountRoleDTO) {
        return accountLogicService.bindRole(accountRoleDTO);
    }

    @GetMapping(value = "")
    public RestResult<AccountDTO> selectByUkId(@RequestParam(value = "ukId") String ukId) {
        return accountLogicService.selectByUkId(ukId);
    }

    @GetMapping(value = "/list")
    public RestResult<List<AccountDTO>> selectListByUsername(@RequestParam(required = false, value = "username") String username,
                                                             @RequestParam(required = false, value = "page", defaultValue = "1") int page,
                                                             @RequestParam(required = false, value = "size", defaultValue = "30") int size) {
        return accountLogicService.selectListByUsername(username, page, size);
    }

    @GetMapping(value = "/details/list")
    public RestResult<List<AccountDetailsDTO>> selectListDetailsByUsername(@RequestParam(required = false, value = "username") String username,
                                                                           @RequestParam(required = false, value = "page", defaultValue = "1") int page,
                                                                           @RequestParam(required = false, value = "size", defaultValue = "30") int size) {
        return accountLogicService.selectListDetailsByUsername(username, page, size);
    }

    @GetMapping(value = "/list/role")
    public RestResult<List<AccountDTO>> selectListByRoleUkId(@RequestParam(value = "roleUkId") String roleUkId) {
        return accountLogicService.selectListByRoleUkId(roleUkId);
    }

    @GetMapping(value = "/list/org")
    public RestResult<List<AccountDTO>> selectListByOrgUkId(@RequestParam(value = "orgUkId") String orgUkId) {
        return accountLogicService.selectListByOrgUkId(orgUkId);
    }


    @GetMapping(value = "/details/list/org")
    public RestResult<List<AccountDetailsDTO>> selectListDetailsByOrgUkId(@RequestParam(value = "orgUkId") String orgUkId) {
        return accountLogicService.selectListDetailsByOrgUkId(orgUkId);
    }

}
